package com.gh.ok.viewmodel

import com.gh.ok.api.Api
import com.gh.ok.entity.UserInfo
import com.ok.common.base.BaseViewModel
import com.ok.common.ext.observer
import kotlinx.coroutines.flow.MutableSharedFlow

class HttpSampleViewModel : BaseViewModel() {

    val userInfo = MutableSharedFlow<UserInfo?>()

    fun getUserInfo(
        loadingText: String = "加载中...",
        loading: Boolean = false,
        loadingDelay: Long = 0,
    ) {
        launch {
            Api.get().geUserInfo()
                .observer(
                    model = this@HttpSampleViewModel,
                    loadingText = loadingText,
                    loading = loading,
                    loadingDelay = loadingDelay,
                ).collect {
                    userInfo.emit(it)
                }
        }
    }

    fun getUserInfo() {
        launch {
            Api.get().geUserInfo()
                .observer(
                    model = this@HttpSampleViewModel,
                ).collect {
                    userInfo.emit(it)
                }
        }
    }

}